package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.ui.lite.BaseUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.com.config.StaticIni;
	import newx.com.control.button.NxButton;
	import newx.com.control.radio.NxRadio;
	import newx.com.control.radio.NxRadioEvent;
	import newx.com.control.radio.NxRadioGroup;
	import newx.com.control.text.NxLabel;
	
	public class QuestionItemUI extends BaseUI
	{
		private static var self:QuestionItemUI;
		private var radioArr:Array	=	[];
		private var radioGroup:NxRadioGroup;
		private var questionObj:Object;
		private var questionNum:int;
		private var doubleAward:int;
		private var model:ModelLocator;
		private var questionIndex:int;
		public var CurCount:int	=	-1;
//		private var bindIndex:int	=	-1;
		private var questionExceptArr:Array	=	[];
		
		public var BackBg:MovieClip;
		public var questionNumLbl:NxLabel;
		public var questionName:NxLabel;
		public var question1:NxLabel;
		public var question2:NxLabel;
		public var question3:NxLabel;
		public var radio1:NxRadio;
		public var radio2:NxRadio;
		public var radio3:NxRadio;
		public var awardBtn:NxButton;
		public var submitBtn:NxButton;
		
		
		public function QuestionItemUI(popupUI:Boolean=true)
		{
			super(popupUI);
		}
		
		public static function getInstance():QuestionItemUI
		{
			if(self == null)
			{
				self 		= 	new QuestionItemUI();
			}
			return self;
		}
		
		override public function Init():void
		{
			model		=	ModelLocator.getInstance();
			radioArr	=	[radio1,radio2,radio3];
			radioGroup	=	new NxRadioGroup(radioArr);
			questionNum	=	int(StaticIni.getIniVar("question_info","total"));
			doubleAward	=	int( StaticIni.getIniVar("question_info","doubleAward") );
			model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_GUILD_QUESTION),freshInfo);
//			bindIndex	=	BindManage.getInstance().BindProp(ModelLocator.getInstance().scene.Role,"CurAnswerCount",this,"changeItemIndex");
		}
		
//		public function set changeItemIndex(val:int):void
//		{
//			CurCount	=	val;
//		}
		
		private function freshInfo(e:CustomMsgEvent):void
		{
			var index:int	=	e.pList[0];
			switch(index)
			{
				//正确
				case 1:
					CurCount		=	e.pList[1];
					QuestionUI.getInstance().showMove();
					hide();
					break;
				//错误
				case 2:
					CurCount		=	e.pList[1];
					rollQuestion();
					break;
			}
		}
		
		override protected function BeforeShow():void
		{
			submitBtn.enabled	=	false;
			awardBtn.enabled	=	false;
			question1.buttonMode=question2.buttonMode=question3.buttonMode=true;
			submitBtn.label		=	StaticIni.getSwfLangStr("StrideChip_6")
			addEvents();
			rollQuestion();
		}
		
		private function rollQuestion():void
		{
			radio1.selected	=	radio2.selected	=	radio3.selected = false;
			if(CurCount==5)
			{
				submitBtn.enabled	=	true;
				awardBtn.enabled	=	true;
				hide();
				return;
			}
			questionIndex	=	randRangeInt(1,questionNum);
			questionObj	=	StaticIni.getIniObj( "question_"+ questionIndex );
//			trace("刷新");
//			trace("CurAnswerCount:"+ CurCount);
			questionNumLbl.htmlText	=	StaticIni.getSwfLangStrVar("UIString_1000382",[CurCount<0?1:CurCount+1]);
			questionName.htmlText	=	StaticIni.getSwfLangStr(String(questionObj["title"]));
			question1.htmlText		=	StaticIni.getSwfLangStr(String(questionObj["questionContent1"]));
			question2.htmlText		=	StaticIni.getSwfLangStr(String(questionObj["questionContent2"]));
			question3.htmlText		=	StaticIni.getSwfLangStr(String(questionObj["questionContent3"]));
			
		}
		
		private function randRangeInt(min:Number, max:Number):int
		{
			var randomNum:int = int( Math.random() * (max - min + 1) ) + min;
			if(questionExceptArr.indexOf(randomNum) != -1)
			{
				return randRangeInt(min,max);
			}
			else
			{
				questionExceptArr.push(randomNum);
			}
			return randomNum;
		}
		
		override protected function AfterHide():void
		{
			removeEvents();
			if(questionExceptArr.length >= 40) questionExceptArr =[];
		}
		
		private function addEvents():void
		{
			radioGroup.addEventListener(NxRadioEvent.RADIO_GROUP_CLICK,radioClick);
			awardBtn.addEventListener(MouseEvent.CLICK,awardClick);
			awardBtn.addEventListener(MouseEvent.MOUSE_OVER,awardOver);
			awardBtn.addEventListener(MouseEvent.MOUSE_OUT,awardOut);
			submitBtn.addEventListener(MouseEvent.CLICK,submitClick);
			for(var i:int=1;i<4;i++)
			{
				(this["question" + i ] as NxLabel).addEventListener(MouseEvent.CLICK,onLabelClick);
			}
		}
		
		private function removeEvents():void
		{
			radioGroup.removeEventListener(NxRadioEvent.RADIO_GROUP_CLICK,radioClick);
			awardBtn.removeEventListener(MouseEvent.CLICK,awardClick);
			awardBtn.removeEventListener(MouseEvent.MOUSE_OVER,awardOver);
			awardBtn.removeEventListener(MouseEvent.MOUSE_OUT,awardOut);
			submitBtn.removeEventListener(MouseEvent.CLICK,submitClick);
			for(var i:int=1;i<4;i++)
			{
				(this["question" + i ] as NxLabel).removeEventListener(MouseEvent.CLICK,onLabelClick);
			}
		}
		
		private function onLabelClick(e:MouseEvent):void
		{
			var selectLabel:NxLabel	= e.currentTarget as NxLabel;
			radio1.selected	=	radio2.selected	=	radio3.selected	= false;
			switch(selectLabel)
			{
				case question1:
					radioGroup.selectdNxRadio	=	radio1;
					break;
				case question2:
					radioGroup.selectdNxRadio	=	radio2;
					break;
				case question3:
					radioGroup.selectdNxRadio	=	radio3;
					break;
			}
		}
		
		private function radioClick(e:NxRadioEvent):void
		{
			submitBtn.enabled	=	true;
			awardBtn.enabled	=	true;
		}
		
		private function awardClick(e:MouseEvent):void
		{
			if(model.scene.Role.getMagicCoin() >= doubleAward)
			{
				QuestionUI.getInstance().awardStyle	=	2;
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFFIRM_CHARGE,15,2,questionIndex,radioGroup.selectedIndex+1);
				submitBtn.enabled = awardBtn.enabled = false;
			}
			else
			{
				NoticeUI.getInstance().showInfo( StaticIni.getSwfLangStr("元宝不足") );
			}
		}
		
		private function awardOver(e:MouseEvent):void
		{
			TipsMainLogic.instance.showString(StaticIni.getSwfLangStrVar("UIString_1000379",[doubleAward]));
		}
		
		private function awardOut(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		private function submitClick(e:MouseEvent):void
		{
			if( submitBtn.label!=StaticIni.getSwfLangStr("提交") )
			{
				submitBtn.label		=	StaticIni.getSwfLangStr("提交");
				awardBtn.enabled	=	true;
				QuestionUI.getInstance().startBtn.visible	=	true;
				QuestionUI.getInstance().answerAgainLbl.visible	=	false;
				return;
			}
			QuestionUI.getInstance().awardStyle	=	1;
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFFIRM_CHARGE,15,1,questionIndex,radioGroup.selectedIndex+1);
			submitBtn.enabled = awardBtn.enabled = false;
		}
		
		
		
	}
}